﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 客户端
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void LjBtn1_Click(object sender, EventArgs e)
        {
            Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Thread rd = new Thread(lianjie);
            rd.IsBackground = true;
            Control.CheckForIllegalCrossThreadCalls = false;
            rd.Start(sk);
        }
        Socket sk;
        private void lianjie(object ob) 
        { 
            sk=ob as Socket;
            IPAddress ip = IPAddress.Parse(FwqIpTxtBox1.Text);
            IPEndPoint ipen = new IPEndPoint(ip, Convert.ToInt32(FwqDkTxtBox1.Text));
            sk.Connect(ipen);
            Thread rt = new Thread(jieshou);
            rt.IsBackground = true;
            Control.CheckForIllegalCrossThreadCalls = false;
            rt.Start();
        }
        private void jieshou() 
        {
            while (true) 
            {
                byte[] bt = new byte[1024 * 1024 * 2];
               int r= sk.Receive(bt);
                if (r == 0) 
                {
                    break;
                }
                str = Encoding.Default.GetString(bt, 0, r);
                showAsg(str);
            }
            

        }
        string str;
        private void showAsg(string str) 
        {
            textBox1.AppendText(str + "\r\n");
        }
        int i = 0;
        private void button1_Click(object sender, EventArgs e)
        {
           byte[]bt= Encoding.Default.GetBytes(textBox2.Text);
            textBox1.AppendText("\r\n");
            showAsg("我"+":"+textBox2.Text);
            textBox2.Clear();
            sk.Send(bt);
        }
    }
}
